﻿//----------------------------------------------------------------
// <copyright file="MptConfiguration.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace MptCodeLibrary.Configuration
{
    /// <summary>
    /// wraps all the accessors to the configuration file
    /// </summary>
    public class MptConfiguration : IMptConfiguration
    {
        /// <summary>
        /// Gets the team servers from the configuration file.
        /// </summary>
        public IEnumerable<TeamServerConfiguration> TeamServers
        {
            get
            {
                TeamServerSection teamServerSection =
                    (TeamServerSection) ConfigurationManager.GetSection("teamServerSection");

                List<TeamServerConfiguration> teamServers = new List<TeamServerConfiguration>();

                foreach (TeamServerConfiguration teamServerConfiguration in teamServerSection.GetTeamServers())
                {
                    teamServers.Add(teamServerConfiguration);
                }

                return teamServers;
            }

            set
            {
                IEnumerable<TeamServerConfiguration> ts = value;
                
                System.Configuration.Configuration config =
                    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                TeamServerSection teamServerSection =
                    (TeamServerSection)config.GetSection("teamServerSection");

                foreach (TeamServerConfiguration teamServerConfiguration in teamServerSection.GetTeamServers())
                {
                    teamServerConfiguration.RollupLastUpdated = (from teamServerConfig in ts
                                                                 where
                                                                     teamServerConfiguration.ServerName.Equals(
                                                                     teamServerConfig.ServerName,
                                                                     StringComparison.OrdinalIgnoreCase)
                                                                 select teamServerConfig.RollupLastUpdated).First();
                }
                config.Save();

                ConfigurationManager.RefreshSection("teamServerSection");
            }
        }

        /// <summary>
        /// Gets the team projects from the configuration file.
        /// </summary>
        public IEnumerable<string> TeamProjects
        {
            get
            {
                TeamProjectSection teamProjectSection =
                    (TeamProjectSection)ConfigurationManager.GetSection("teamProjectSection");

                List<string> teamProjects = new List<string>();

                foreach (TeamProjectConfiguration teamProjectConfiguration in teamProjectSection.GetTeamProjects())
                {
                    teamProjects.Add(teamProjectConfiguration.ProjectName);
                }

                return teamProjects;
            }
        }
    }
}
